﻿using System;
using System.Collections.Generic;
using System.Text;
using MSXML2;
using mshtml;
using System.IO;
namespace WebHacker
{
    public class WebTools
    {
        private XMLHTTP wtXMLHTTP;

        public WebTools()
        {
            wtXMLHTTP = new XMLHTTP();
        }

        public HTMLDocument HttpRequest(String Link, RequestHeaders pRequestHeaders)
        {

            try
            {
                //Open
                this.wtXMLHTTP.open("GET", Link, false);

                //Header
                RequestHeaders.Enumerator item = pRequestHeaders.GetEnumerator();
                while (item.MoveNext())
                {

                    this.wtXMLHTTP.setRequestHeader(item.Current.Key, item.Current.Value);
                }

                //Send
                this.wtXMLHTTP.send();
            }catch(FileNotFoundException e){
                throw new FileNotFoundException();
                   
            }

            //404 not found

            //Check
            if (200 != this.wtXMLHTTP.status)//OK
                return null;

            return this.HTMLTextToHTMLDocument(this.wtXMLHTTP.responseText);
        }

        public String HttpRequest_Text(String Link, RequestHeaders pRequestHeaders)
        {

            try
            {
                //Open
                this.wtXMLHTTP.open("GET", Link, false);

                //Header
                RequestHeaders.Enumerator item = pRequestHeaders.GetEnumerator();
                while (item.MoveNext())
                {

                    this.wtXMLHTTP.setRequestHeader(item.Current.Key, item.Current.Value);
                }

                //Send
                this.wtXMLHTTP.send();
            }
            catch (FileNotFoundException e)
            {
                throw new FileNotFoundException();

            }

            //404 not found

            //Check
            if (200 != this.wtXMLHTTP.status)//OK
                return null;

            return this.wtXMLHTTP.responseText;
        }

        //public HTMLDocument SubmitFormData(String Link, HttpRequestDictionary vFormParas)
        //{
        public HTMLDocument SubmitFormData(String Link, string vFormParas)
        {
            this.wtXMLHTTP.open("POST", Link, false);

            this.wtXMLHTTP.setRequestHeader("CONTENT-TYPE", "application/x-www-form-urlencoded");
            //this.wtXMLHTTP.setRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
            //this.wtXMLHTTP.setRequestHeader("Host", "pos.yotsuyaotsuka.net");
            //this.wtXMLHTTP.setRequestHeader("Origin", "http://pos.yotsuyaotsuka.net");

            this.wtXMLHTTP.send(vFormParas);


            switch (this.wtXMLHTTP.status)
            {
                case 200:
                    return this.HTMLTextToHTMLDocument(this.wtXMLHTTP.responseText);
                case 302://??


                    break;
                default:
                    return null;

            }


            return null;
        }


        private HTMLDocument HTMLTextToHTMLDocument(String pTextHtml)
        {
            //Cast to Html
            object[] oPageText = { pTextHtml };
            HTMLDocument doc = new HTMLDocumentClass();
            IHTMLDocument2 doc2 = (IHTMLDocument2)doc;

			doc2.designMode = "on";
            doc2.write(oPageText);
            return doc2 as HTMLDocument;
        }

    }
}
